﻿// -----------------------------------------------------------------------
// <copyright file="Compilation.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Raop.Library.Tools
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Roslyn.Compilers.CSharp;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class CompilationExtention
    {
        public static IEnumerable<SemanticModel> GetModels(this Compilation compil)
        {
            foreach (SyntaxTree sourceTree in compil.SyntaxTrees)
            {
                yield return compil.GetSemanticModel(sourceTree);
            }
        }

        public static IEnumerable<NamedTypeSymbol> FindNamedTypeSymboles(this Compilation compil, Func<NamedTypeSymbol, bool> filter = null)
        {
            foreach (var item in compil.GetModels())
            {
                var visitors = new ClassDeclarationSyntaxVisitors();

                var syntaxTree = item.SyntaxTree;

                var root = syntaxTree.Root;

                var collections = visitors.Visit(root);

                if (collections != null)
                {
                    foreach (var syntaxe in collections)
                    {
                        yield return item.GetDeclaredSymbol(syntaxe);
                    }
                }
            }
        }

        public class ClassDeclarationSyntaxVisitors : SyntaxVisitor<List<ClassDeclarationSyntax>>
        {
            protected override List<ClassDeclarationSyntax> VisitClassDeclaration(ClassDeclarationSyntax node)
            {
                return new List<ClassDeclarationSyntax> { node };
            }
        }
    }
}
